/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AutoProvMode;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.EntryCacheDataKey;
/*     */ import com.zimbra.cs.account.Provisioning.DirectoryEntryVisitor;
/*     */ import com.zimbra.cs.account.names.NameUtil.EmailAddress;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapInvalidAttrValueException;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.activation.DataSource;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.internet.AddressException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class AutoProvision
/*     */ {
/*     */   protected LdapProv prov;
/*     */   protected Domain domain;
/*     */   
/*     */   protected AutoProvision(LdapProv prov, Domain domain)
/*     */   {
/*  87 */     this.prov = prov;
/*  88 */     this.domain = domain;
/*     */   }
/*     */   
/*     */   abstract Account handle()
/*     */     throws ServiceException;
/*     */   
/*     */   protected Account createAccount(String acctZimbraName, ExternalEntry externalEntry, String password, ZAttrProvisioning.AutoProvMode mode) throws ServiceException
/*     */   {
/*  96 */     ZAttributes externalAttrs = externalEntry.getAttrs();
/*     */     
/*  98 */     Map<String, Object> zimbraAttrs = mapAttrs(externalAttrs);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 107 */     String zimbraPassword = null;
/* 108 */     if (password != null) {
/* 109 */       zimbraPassword = password;
/* 110 */       zimbraAttrs.remove("userPassword");
/*     */     }
/*     */     
/* 113 */     Account acct = null;
/*     */     try
/*     */     {
/* 116 */       acct = this.prov.createAccount(acctZimbraName, zimbraPassword, zimbraAttrs);
/*     */     } catch (ServiceException e) {
/* 118 */       if ("account.ACCOUNT_EXISTS".equals(e.getCode())) {
/* 119 */         ZimbraLog.autoprov.debug("account %s already exists", new Object[] { acctZimbraName });
/*     */         
/* 121 */         switch (mode) {
/*     */         case EAGER: 
/* 123 */           return null;
/*     */         }
/*     */         
/*     */         
/* 127 */         throw e;
/*     */       }
/*     */       
/* 130 */       throw e;
/*     */     }
/*     */     
/*     */ 
/* 134 */     ZimbraLog.autoprov.info("auto provisioned account: " + acctZimbraName);
/*     */     
/* 136 */     ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "auto provision Account", "name", acct.getName(), "id", acct.getId() }, zimbraAttrs));
/*     */     
/*     */ 
/*     */ 
/*     */     try
/*     */     {
/* 142 */       sendNotifMessage(acct, zimbraPassword);
/*     */     }
/*     */     catch (ServiceException e) {
/* 145 */       ZimbraLog.autoprov.warn("unable to send auto provision notification email", e);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 150 */       AutoProvisionListener listener = AutoProvisionCachedInfo.access$000(this.domain).getListener();
/* 151 */       if (listener != null) {
/* 152 */         listener.postCreate(this.domain, acct, externalEntry.getDN());
/*     */ 
/*     */       }
/* 155 */       else if (mode == ZAttrProvisioning.AutoProvMode.EAGER) {
/* 156 */         ZimbraLog.autoprov.warn("EAGER mode should configure zimbraAutoProvListenerClass");
/*     */       }
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/* 161 */       ZimbraLog.autoprov.warn("encountered error in post auto provision listener", e);
/*     */     }
/*     */     
/* 164 */     return acct;
/*     */   }
/*     */   
/*     */   private static class AutoProvisionCachedInfo {
/*     */     private static final String DELIMITER = "=";
/*     */     
/* 170 */     private static AutoProvisionCachedInfo getInfo(Domain domain) throws ServiceException { AutoProvisionCachedInfo attrMap = (AutoProvisionCachedInfo)domain.getCachedData(EntryCacheDataKey.DOMAIN_AUTO_PROVISION_DATA);
/*     */       
/*     */ 
/* 173 */       if (attrMap == null) {
/* 174 */         attrMap = new AutoProvisionCachedInfo(domain);
/* 175 */         domain.setCachedData(EntryCacheDataKey.DOMAIN_AUTO_PROVISION_DATA, attrMap);
/*     */       }
/*     */       
/* 178 */       return attrMap;
/*     */     }
/*     */     
/*     */ 
/* 182 */     private final Map<String, String> attrMap = new HashMap();
/*     */     private final String[] attrsToFetch;
/*     */     private AutoProvisionListener listener;
/*     */     
/*     */     private AutoProvisionCachedInfo(Domain domain) throws ServiceException {
/* 187 */       AttributeManager attrMgr = AttributeManager.getInstance();
/*     */       
/*     */ 
/* 190 */       Set<String> validAccountAttrs = attrMgr.getAllAttrsInClass(AttributeClass.account);
/*     */       
/* 192 */       String[] rules = domain.getAutoProvAttrMap();
/*     */       
/* 194 */       for (String rule : rules) {
/* 195 */         String[] parts = rule.split("=");
/* 196 */         if (parts.length != 2) {
/* 197 */           throw ServiceException.FAILURE("invalid value in zimbraAutoProvAttrMap: " + rule, null);
/*     */         }
/*     */         
/*     */ 
/* 201 */         String externalAttr = parts[0];
/* 202 */         String zimbraAttr = parts[1];
/*     */         
/* 204 */         if (!validAccountAttrs.contains(zimbraAttr)) {
/* 205 */           throw ServiceException.FAILURE("invalid value in zimbraAutoProvAttrMap: " + rule + ", not a valid zimbra attribute ", null);
/*     */         }
/*     */         
/*     */ 
/*     */ 
/* 210 */         this.attrMap.put(externalAttr, zimbraAttr);
/*     */       }
/*     */       
/* 213 */       Set<String> attrs = new HashSet();
/* 214 */       attrs.addAll(this.attrMap.keySet());
/* 215 */       attrs.add("createTimestamp");
/* 216 */       String nameMapAttr = domain.getAutoProvAccountNameMap();
/* 217 */       if (nameMapAttr != null) {
/* 218 */         attrs.add(nameMapAttr);
/*     */       }
/* 220 */       this.attrsToFetch = ((String[])attrs.toArray(new String[0]));
/*     */       
/*     */ 
/* 223 */       String className = domain.getAutoProvListenerClass();
/* 224 */       if (className != null) {
/*     */         try {
/* 226 */           if (className != null) {
/* 227 */             this.listener = ((AutoProvisionListener)ExtensionUtil.findClass(className).asSubclass(AutoProvisionListener.class).newInstance());
/*     */           }
/*     */         }
/*     */         catch (ClassNotFoundException e) {
/* 231 */           ZimbraLog.autoprov.warn("unable to find auto provision listener class " + className, e);
/*     */         }
/*     */         catch (InstantiationException e) {
/* 234 */           ZimbraLog.autoprov.warn("unable to instantiate auto provision listener object of class " + className, e);
/*     */         }
/*     */         catch (IllegalAccessException e)
/*     */         {
/* 238 */           ZimbraLog.autoprov.warn("unable to instantiate auto provision listener object of class " + className, e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */     private String getZimbraAttrName(String externalAttrName)
/*     */     {
/* 246 */       return (String)this.attrMap.get(externalAttrName);
/*     */     }
/*     */     
/*     */     private String[] getAttrsToFetch() {
/* 250 */       return this.attrsToFetch;
/*     */     }
/*     */     
/*     */     private AutoProvisionListener getListener() {
/* 254 */       return this.listener;
/*     */     }
/*     */   }
/*     */   
/*     */   protected String[] getAttrsToFetch() throws ServiceException {
/* 259 */     return AutoProvisionCachedInfo.access$000(this.domain).getAttrsToFetch();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected String mapName(ZAttributes externalAttrs, String loginName)
/*     */     throws ServiceException
/*     */   {
/* 271 */     String localpart = null;
/*     */     
/* 273 */     String localpartAttr = this.domain.getAutoProvAccountNameMap();
/* 274 */     if (localpartAttr != null) {
/* 275 */       localpart = externalAttrs.getAttrString(localpartAttr);
/* 276 */       if (localpart == null) {
/* 277 */         throw ServiceException.FAILURE("AutoProvision: unable to get localpart: " + loginName, null);
/*     */       }
/*     */     }
/*     */     else {
/* 281 */       if (loginName == null) {
/* 282 */         throw ServiceException.FAILURE("AutoProvision: unable to map acount name, must configure zimbraAutoProvAccountNameMap", null);
/*     */       }
/*     */       
/*     */ 
/* 286 */       NameUtil.EmailAddress emailAddr = new NameUtil.EmailAddress(loginName, false);
/* 287 */       localpart = emailAddr.getLocalPart();
/*     */     }
/*     */     
/* 290 */     return localpart + "@" + this.domain.getName();
/*     */   }
/*     */   
/*     */   protected Map<String, Object> mapAttrs(ZAttributes externalAttrs)
/*     */     throws ServiceException
/*     */   {
/* 296 */     AutoProvisionCachedInfo attrMap = AutoProvisionCachedInfo.getInfo(this.domain);
/*     */     
/* 298 */     Map<String, Object> extAttrs = externalAttrs.getAttrs();
/* 299 */     Map<String, Object> zimbraAttrs = new HashMap();
/*     */     
/* 301 */     for (Map.Entry<String, Object> extAttr : extAttrs.entrySet()) {
/* 302 */       String extAttrName = (String)extAttr.getKey();
/* 303 */       Object attrValue = extAttr.getValue();
/*     */       
/* 305 */       String zimbraAttrName = attrMap.getZimbraAttrName(extAttrName);
/* 306 */       if (zimbraAttrName != null) {
/* 307 */         if ((attrValue instanceof String)) {
/* 308 */           StringUtil.addToMultiMap(zimbraAttrs, zimbraAttrName, (String)attrValue);
/* 309 */         } else if ((attrValue instanceof String[])) {
/* 310 */           for (String value : (String[])attrValue) {
/* 311 */             StringUtil.addToMultiMap(zimbraAttrs, zimbraAttrName, value);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 317 */     return zimbraAttrs;
/*     */   }
/*     */   
/*     */   protected ZAttributes getExternalAttrsByDn(String dn) throws ServiceException {
/* 321 */     String url = this.domain.getAutoProvLdapURL();
/* 322 */     boolean wantStartTLS = this.domain.isAutoProvLdapStartTlsEnabled();
/* 323 */     String adminDN = this.domain.getAutoProvLdapAdminBindDn();
/* 324 */     String adminPassword = this.domain.getAutoProvLdapAdminBindPassword();
/*     */     
/* 326 */     LdapServerConfig.ExternalLdapConfig config = new LdapServerConfig.ExternalLdapConfig(url, wantStartTLS, null, adminDN, adminPassword, null, "auto provision account");
/*     */     
/*     */ 
/* 329 */     ZLdapContext zlc = null;
/*     */     try
/*     */     {
/* 332 */       zlc = LdapClient.getExternalContext(config, LdapUsage.AUTO_PROVISION);
/* 333 */       return this.prov.getHelper().getAttributes(zlc, dn, getAttrsToFetch());
/*     */     } finally {
/* 335 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   protected static class ExternalEntry {
/*     */     private final String dn;
/*     */     private final ZAttributes attrs;
/*     */     
/*     */     ExternalEntry(String dn, ZAttributes attrs) {
/* 344 */       this.dn = dn;
/* 345 */       this.attrs = attrs;
/*     */     }
/*     */     
/*     */     String getDN() {
/* 349 */       return this.dn;
/*     */     }
/*     */     
/*     */     ZAttributes getAttrs() {
/* 353 */       return this.attrs;
/*     */     }
/*     */   }
/*     */   
/*     */   protected ExternalEntry getExternalAttrsByName(String loginName) throws ServiceException
/*     */   {
/* 359 */     String url = this.domain.getAutoProvLdapURL();
/* 360 */     boolean wantStartTLS = this.domain.isAutoProvLdapStartTlsEnabled();
/* 361 */     String adminDN = this.domain.getAutoProvLdapAdminBindDn();
/* 362 */     String adminPassword = this.domain.getAutoProvLdapAdminBindPassword();
/* 363 */     String[] attrs = getAttrsToFetch();
/*     */     
/*     */ 
/* 366 */     LdapServerConfig.ExternalLdapConfig config = new LdapServerConfig.ExternalLdapConfig(url, wantStartTLS, null, adminDN, adminPassword, null, "auto provision account");
/*     */     
/*     */ 
/* 369 */     ZLdapContext zlc = null;
/*     */     try
/*     */     {
/* 372 */       zlc = LdapClient.getExternalContext(config, LdapUsage.AUTO_PROVISION);
/*     */       
/* 374 */       String searchFilterTemplate = this.domain.getAutoProvLdapSearchFilter();
/* 375 */       ZSearchResultEntry entry; if (searchFilterTemplate != null)
/*     */       {
/* 377 */         String searchBase = this.domain.getAutoProvLdapSearchBase();
/* 378 */         if (searchBase == null) {
/* 379 */           searchBase = "";
/*     */         }
/* 381 */         String searchFilter = LdapUtil.computeDn(loginName, searchFilterTemplate);
/* 382 */         ZimbraLog.autoprov.debug("AutoProvision: computed search filter" + searchFilter);
/* 383 */         entry = this.prov.getHelper().searchForEntry(searchBase, ZLdapFilterFactory.getInstance().fromFilterString(ZLdapFilterFactory.FilterId.AUTO_PROVISION_SEARCH, searchFilter), zlc, attrs);
/*     */         
/*     */ 
/*     */ 
/* 387 */         if (entry == null) {
/* 388 */           throw AccountServiceException.NO_SUCH_EXTERNAL_ENTRY(loginName);
/*     */         }
/* 390 */         return new ExternalEntry(entry.getDN(), entry.getAttributes());
/*     */       }
/*     */       
/* 393 */       String bindDNTemplate = this.domain.getAutoProvLdapBindDn();
/* 394 */       if (bindDNTemplate != null)
/*     */       {
/* 396 */         String dn = LdapUtil.computeDn(loginName, bindDNTemplate);
/* 397 */         ZimbraLog.autoprov.debug("AutoProvision: computed external DN" + dn);
/* 398 */         return new ExternalEntry(dn, this.prov.getHelper().getAttributes(zlc, dn, attrs));
/*     */       }
/*     */     }
/*     */     finally {
/* 402 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */     
/* 405 */     throw ServiceException.FAILURE("One of zimbraAutoProvLdapBindDn or zimbraAutoProvLdapSearchFilter must be set", null);
/*     */   }
/*     */   
/*     */   private String fillTemplate(Account acct, String template)
/*     */   {
/* 410 */     String text = template.replaceAll("\\$\\{ACCOUNT_ADDRESS\\}", acct.getName());
/*     */     
/* 412 */     String displayName = acct.getDisplayName();
/* 413 */     if (displayName == null) {
/* 414 */       displayName = "";
/*     */     }
/* 416 */     text = text.replaceAll("\\$\\{ACCOUNT_DISPLAY_NAME\\}", displayName);
/* 417 */     return text;
/*     */   }
/*     */   
/*     */   protected void sendNotifMessage(Account acct, String password) throws ServiceException
/*     */   {
/* 422 */     String subject = fillTemplate(acct, this.domain.getAutoProvNotificationSubject());
/* 423 */     String body = fillTemplate(acct, this.domain.getAutoProvNotificationBody());
/*     */     
/* 425 */     String from = this.domain.getAutoProvNotificationFromAddress();
/* 426 */     if (from == null)
/*     */     {
/*     */ 
/* 429 */       return;
/*     */     }
/*     */     
/* 432 */     String toAddr = acct.getName();
/*     */     try
/*     */     {
/* 435 */       SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/*     */       
/* 437 */       InternetAddress addr = null;
/*     */       try {
/* 439 */         addr = new JavaMailInternetAddress(from);
/*     */       }
/*     */       catch (AddressException e) {
/* 442 */         ZimbraLog.autoprov.warn("invalid address in zimbraAutoProvNotificationFromAddress", e);
/*     */       }
/*     */       
/*     */ 
/* 446 */       Address fromAddr = addr;
/* 447 */       Address replyToAddr = addr;
/*     */       
/*     */ 
/* 450 */       out.setFrom(fromAddr);
/*     */       
/*     */ 
/* 453 */       out.setReplyTo(new Address[] { replyToAddr });
/*     */       
/*     */ 
/*     */ 
/* 457 */       out.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
/*     */       
/*     */ 
/* 460 */       out.setSentDate(new Date());
/*     */       
/*     */ 
/* 463 */       Locale locale = acct.getLocale();
/* 464 */       out.setSubject(subject);
/*     */       
/*     */ 
/* 467 */       out.setNotifyOptions(-1);
/*     */       
/*     */ 
/* 470 */       MimeMultipart mmp = new ZMimeMultipart("alternative");
/*     */       
/*     */ 
/* 473 */       String text = body;
/* 474 */       MimeBodyPart textPart = new ZMimeBodyPart();
/* 475 */       textPart.setText(text, "utf-8");
/* 476 */       mmp.addBodyPart(textPart);
/*     */       
/*     */ 
/* 479 */       StringBuilder html = new StringBuilder();
/* 480 */       html.append("<h4>\n");
/* 481 */       html.append("<p>" + body + "</p>\n");
/* 482 */       html.append("</h4>\n");
/* 483 */       html.append("\n");
/*     */       
/* 485 */       MimeBodyPart htmlPart = new ZMimeBodyPart();
/* 486 */       htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
/* 487 */       mmp.addBodyPart(htmlPart);
/* 488 */       out.setContent(mmp);
/*     */       
/*     */ 
/* 491 */       Transport.send(out);
/*     */       
/*     */ 
/* 494 */       Address[] rcpts = out.getRecipients(Message.RecipientType.TO);
/* 495 */       StringBuilder rcptAddr = new StringBuilder();
/* 496 */       for (Address a : rcpts)
/* 497 */         rcptAddr.append(a.toString());
/* 498 */       ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
/*     */     }
/*     */     catch (MessagingException e)
/*     */     {
/* 502 */       ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static abstract class MimePartDataSource implements DataSource
/*     */   {
/*     */     private final String mText;
/* 509 */     private byte[] mBuf = null;
/*     */     
/*     */     public MimePartDataSource(String text) {
/* 512 */       this.mText = text;
/*     */     }
/*     */     
/*     */     public InputStream getInputStream() throws IOException
/*     */     {
/* 517 */       synchronized (this) {
/* 518 */         if (this.mBuf == null) {
/* 519 */           ByteArrayOutputStream buf = new ByteArrayOutputStream();
/* 520 */           OutputStreamWriter wout = new OutputStreamWriter(buf, "utf-8");
/*     */           
/* 522 */           String text = this.mText;
/* 523 */           wout.write(text);
/* 524 */           wout.flush();
/* 525 */           this.mBuf = buf.toByteArray();
/*     */         }
/*     */       }
/* 528 */       ByteArrayInputStream in = new ByteArrayInputStream(this.mBuf);
/* 529 */       return in;
/*     */     }
/*     */     
/*     */     public OutputStream getOutputStream()
/*     */     {
/* 534 */       throw new UnsupportedOperationException();
/*     */     }
/*     */   }
/*     */   
/*     */   private static class HtmlPartDataSource extends AutoProvision.MimePartDataSource
/*     */   {
/*     */     private static final String CONTENT_TYPE = "text/html; charset=utf-8";
/*     */     private static final String NAME = "HtmlDataSource";
/*     */     
/*     */     HtmlPartDataSource(String text) {
/* 544 */       super();
/*     */     }
/*     */     
/*     */     public String getContentType()
/*     */     {
/* 549 */       return "text/html; charset=utf-8";
/*     */     }
/*     */     
/*     */     public String getName()
/*     */     {
/* 554 */       return "HtmlDataSource";
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static void searchAutoProvDirectory(LdapProv prov, Domain domain, String filter, String name, String createTimestampLaterThan, String[] returnAttrs, int maxResults, Provisioning.DirectoryEntryVisitor visitor)
/*     */     throws ServiceException
/*     */   {
/* 566 */     SearchLdapOptions.SearchLdapVisitor ldapVisitor = new SearchLdapOptions.SearchLdapVisitor()
/*     */     {
/*     */       public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) throws SearchLdapOptions.StopIteratingException
/*     */       {
/* 570 */         this.val$visitor.visit(dn, attrs);
/*     */       }
/*     */       
/* 573 */     };
/* 574 */     searchAutoProvDirectory(prov, domain, filter, name, createTimestampLaterThan, returnAttrs, maxResults, ldapVisitor, false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static boolean searchAutoProvDirectory(LdapProv prov, Domain domain, String filter, String name, String createTimestampLaterThan, String[] returnAttrs, int maxResults, SearchLdapOptions.SearchLdapVisitor ldapVisitor, boolean wantPartialResult)
/*     */     throws ServiceException
/*     */   {
/* 615 */     if ((filter != null) && (name != null)) {
/* 616 */       throw ServiceException.INVALID_REQUEST("only one of filter or name can be provided", null);
/*     */     }
/*     */     
/* 619 */     String url = domain.getAutoProvLdapURL();
/* 620 */     boolean wantStartTLS = domain.isAutoProvLdapStartTlsEnabled();
/* 621 */     String adminDN = domain.getAutoProvLdapAdminBindDn();
/* 622 */     String adminPassword = domain.getAutoProvLdapAdminBindPassword();
/* 623 */     String searchBase = domain.getAutoProvLdapSearchBase();
/* 624 */     String searchFilterTemplate = domain.getAutoProvLdapSearchFilter();
/* 625 */     ZLdapFilterFactory.FilterId filterId = ZLdapFilterFactory.FilterId.AUTO_PROVISION_SEARCH;
/*     */     
/* 627 */     if (url == null) {
/* 628 */       throw ServiceException.FAILURE(String.format("missing %s on domain %s", new Object[] { "zimbraAutoProvLdapURL", domain.getName() }), null);
/*     */     }
/*     */     
/* 631 */     if (searchBase == null) {
/* 632 */       searchBase = "";
/*     */     }
/*     */     
/* 635 */     LdapServerConfig.ExternalLdapConfig config = new LdapServerConfig.ExternalLdapConfig(url, wantStartTLS, null, adminDN, adminPassword, null, "search auto provision directory");
/*     */     
/*     */ 
/* 638 */     boolean hitSizeLimitExceededException = false;
/* 639 */     ZLdapContext zlc = null;
/* 640 */     ZLdapFilter zFilter = null;
/*     */     try {
/* 642 */       zlc = LdapClient.getExternalContext(config, LdapUsage.AUTO_PROVISION_ADMIN_SEARCH);
/*     */       
/* 644 */       String searchFilter = null;
/* 645 */       String searchFilterWithoutLastPolling = null;
/*     */       
/* 647 */       if (name != null) {
/* 648 */         if (searchFilterTemplate == null) {
/* 649 */           throw ServiceException.INVALID_REQUEST("search filter template is not set on domain " + domain.getName(), null);
/*     */         }
/*     */         
/* 652 */         searchFilter = LdapUtil.computeDn(name, searchFilterTemplate);
/* 653 */       } else if (filter != null) {
/* 654 */         searchFilter = filter;
/* 655 */         filterId = ZLdapFilterFactory.FilterId.AUTO_PROVISION_ADMIN_SEARCH;
/*     */       } else {
/* 657 */         if (searchFilterTemplate == null) {
/* 658 */           throw ServiceException.INVALID_REQUEST("search filter template is not set on domain " + domain.getName(), null);
/*     */         }
/*     */         
/* 661 */         searchFilter = LdapUtil.computeDn("*", searchFilterTemplate);
/* 662 */         if (createTimestampLaterThan != null) {
/* 663 */           searchFilterWithoutLastPolling = searchFilter;
/*     */           
/* 665 */           searchFilter = "(&" + searchFilter + ZLdapFilterFactory.getInstance().createdLaterOrEqual(createTimestampLaterThan).toFilterString() + ")";
/*     */           
/* 667 */           filterId = ZLdapFilterFactory.FilterId.AUTO_PROVISION_SEARCH_CREATED_LATERTHAN;
/*     */         }
/*     */       }
/*     */       
/* 671 */       zFilter = ZLdapFilterFactory.getInstance().fromFilterString(filterId, searchFilter);
/*     */       try
/*     */       {
/* 674 */         searchOptions = new SearchLdapOptions(searchBase, zFilter, returnAttrs, maxResults, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, ldapVisitor);
/*     */         
/* 676 */         zlc.searchPaged(searchOptions);
/*     */       } catch (LdapException.LdapInvalidAttrValueException eav) {
/* 678 */         ZimbraLog.autoprov.info("Retrying ldap search query with createTimestamp in seconds.");
/* 679 */         if ((searchFilterWithoutLastPolling != null) && (createTimestampLaterThan != null)) {
/* 680 */           createTimestampLaterThan = createTimestampLaterThan.replaceAll("\\..*Z$", "Z");
/*     */           
/* 682 */           searchFilter = "(&" + searchFilterWithoutLastPolling + ZLdapFilterFactory.getInstance().createdLaterOrEqual(createTimestampLaterThan).toFilterString() + ")";
/*     */           
/* 684 */           ZimbraLog.autoprov.info("new searchFilter = %s", new Object[] { searchFilter });
/* 685 */           filterId = ZLdapFilterFactory.FilterId.AUTO_PROVISION_SEARCH_CREATED_LATERTHAN;
/*     */         }
/* 687 */         zFilter = ZLdapFilterFactory.getInstance().fromFilterString(filterId, searchFilter);
/* 688 */         SearchLdapOptions searchOptions = new SearchLdapOptions(searchBase, zFilter, returnAttrs, maxResults, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, ldapVisitor);
/*     */         
/* 690 */         zlc.searchPaged(searchOptions);
/*     */       }
/*     */     } catch (LdapException.LdapSizeLimitExceededException e) {
/* 693 */       hitSizeLimitExceededException = true;
/* 694 */       if (wantPartialResult)
/*     */       {
/* 696 */         ZimbraLog.autoprov.debug(String.format("searchAutoProvDirectory encountered LdapSizeLimitExceededException: base=%s, filter=%s", new Object[] { searchBase, zFilter == null ? "" : zFilter.toFilterString() }), e);
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 701 */         throw AccountServiceException.TOO_MANY_SEARCH_RESULTS("too many search results returned", e);
/*     */       }
/*     */     } finally {
/* 704 */       LdapClient.closeContext(zlc);
/*     */     }
/* 706 */     return hitSizeLimitExceededException;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/AutoProvision.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */